﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Sekretar.Repository;
using System.Linq;
using Sekretar.DataLayer;

namespace Sekretar
{
    public partial class MestoRecView : Common.Forms.RecView
    {
        RMesto Mesto = new RMesto();
        ROpstina rOpstina = new ROpstina();
        RDrzava rDrzava = new RDrzava();
        int id;

        public MestoRecView(int id)
        {
            InitializeComponent();
            this.id = id;
        }

        private void MestoRecView_Load(object sender, EventArgs e)
        {
            bsOpstina.DataSource = rOpstina.VratiSveAktivne();

            bsDrzava.DataSource = rDrzava.VratiSveAktivne();

            if (id == 0)
                MestoBindingSource.DataSource = Mesto.Nov();
            else
                MestoBindingSource.DataSource = Mesto.Izmeni(this.id);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                MestoBindingSource.EndEdit();
                int i = Mesto.Snimi((DataLayer.Mesto)MestoBindingSource.Current);
                this.Index = Mesto.VratiSve().ToList().FindIndex(c => c.MestoId == i);
            }
            catch (Exception ex)
            {
                Common.Tools.MessageError.Show(ex.Message);
                this.DialogResult = DialogResult.None;
            }
        }

        private void gridLookUpEdit1_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                OpstinaRecView f = new OpstinaRecView(0);
                if (f.ShowDialog() == DialogResult.OK)
                {
                    bsOpstina.DataSource = rOpstina.VratiSve();
                    ((Mesto)MestoBindingSource.Current).OpstinaId = f.ID;
                }
            }
        }

        private void gridLookUpEdit2_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Index == 1)
            {
                DrzavaRecView f = new DrzavaRecView(0);
                if (f.ShowDialog() == DialogResult.OK)
                {
                    bsDrzava.DataSource = rDrzava.VratiSve();
                    ((Mesto)MestoBindingSource.Current).DrzavaId = f.ID;
                }
            }
        }
    }
}
